package logic

import (
	"context"
	"errors"
	"person/models/mysql"

	"person/personrpc/internal/svc"
	"person/personrpc/personrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateUserLogic) UpdateUser(in *personrpc.UpdateUserRequest) (*personrpc.UpdateUserResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User

	if err := user.FindUserByUserId(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}
	user.Username = in.Username
	user.Marriage = in.Marriage
	user.CardImage = in.CardImage
	user.Card = in.Card
	user.Sex = in.Sex
	user.Birth = in.Birth
	user.Phone = in.Phone
	if err := user.UpdateUser(mysql.DB); err != nil {
		return nil, err
	}

	return &personrpc.UpdateUserResponse{
		UserId: int64(user.ID),
	}, nil
}
